package com.christianposta.learnddd.domain.model.cargo;

import java.util.Date;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.christianposta.learnddd.domain.model.location.Location;
import com.christianposta.learnddd.domain.model.voyage.Voyage;
import com.christianposta.learnddd.domain.shared.ValueObject;

public class Leg implements ValueObject<Leg>{

	private Voyage voyage;
	private Location loadLocation;
	private Location unloadLocation;
	private Date loadDate;
	private Date unloadDate;
	
	
	
	public Leg(Voyage voyage, Location loadLocation, Location unloadLocation,
			Date loadDate, Date unloadDate) {
		Validate.noNullElements(new Object[] {voyage, loadLocation, unloadLocation, loadDate, unloadDate});
		
		this.voyage = voyage;
		this.loadLocation = loadLocation;
		this.unloadLocation = unloadLocation;
		this.loadDate = loadDate;
		this.unloadDate = unloadDate;
	}


	public Voyage voyage(){
		return this.voyage;
	}
	
	public Location loadLocation(){
		return this.loadLocation;
	}
	
	public Location unloadLocation(){
		return this.unloadLocation;
	}
	
	public Date loadDate(){
		return new Date(this.loadDate.getTime());
	}
	
	public Date unloadDate(){
		return new Date(this.unloadDate.getTime());
	}

	public boolean sameValueAs(Leg other) {
		return other != null &&
			new EqualsBuilder().append(this.voyage, other.voyage)
				.append(this.loadLocation, other.loadLocation)
				.append(this.unloadLocation, other.unloadLocation)
				.append(this.loadDate, other.loadDate)
				.append(this.unloadDate, other.unloadDate)
				.isEquals();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == this) return true;
		if(obj != null && obj.getClass() != this.getClass()) return false;
		
		final Leg other = (Leg) obj;
		
		return sameValueAs(other);
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.voyage)
			.append(this.loadLocation)
			.append(this.unloadLocation)
			.append(this.loadDate)
			.append(this.unloadLocation)
			.toHashCode();
	}
}
